package org.wyf.EveryPoint.crowd.controller;

import org.springframework.http.HttpStatus;
import org.wyf.EveryPoint.common.annotation.ControllerEndpoint;
import org.wyf.EveryPoint.common.controller.BaseController;
import org.wyf.EveryPoint.common.entity.FebsConstant;
import org.wyf.EveryPoint.common.entity.FebsResponse;
import org.wyf.EveryPoint.common.entity.QueryRequest;
import org.wyf.EveryPoint.common.utils.FebsUtil;
import org.wyf.EveryPoint.crowd.entity.Team;
import org.wyf.EveryPoint.crowd.service.ITeamService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.wyf.EveryPoint.system.entity.User;
import org.wyf.EveryPoint.system.service.IUserService;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 项目小组 Controller
 *
 * @author wyf
 * @date 2021-04-09 16:44:58
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class TeamController extends BaseController {

    private final ITeamService teamService;
    private final IUserService userService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "team")
    public String teamIndex(){
        return FebsUtil.view("team/team");
    }

    @GetMapping("team")
    @ResponseBody
    @RequiresPermissions("team:list")
    public FebsResponse getAllTeams(Team team) {
        return new FebsResponse().success().data(teamService.findTeams(team));
    }

    @GetMapping("team/list")
    @ResponseBody
    @RequiresPermissions("team:list")
    public FebsResponse teamList(QueryRequest request, Team team) {
        Map<String, Object> dataTable = getDataTable(this.teamService.findTeams(request, team));
        return new FebsResponse().success().data(dataTable);
    }

    @ControllerEndpoint(operation = "新增Team", exceptionMessage = "新增Team失败")
    @PostMapping("team")
    @ResponseBody
    @RequiresPermissions("team:add")
    public FebsResponse addTeam(@Valid Team team) {
        this.teamService.createTeam(team);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "删除Team", exceptionMessage = "删除Team失败")
    @GetMapping("team/delete")
    @ResponseBody
    @RequiresPermissions("team:delete")
    public FebsResponse deleteTeam(Team team) {
        this.teamService.deleteTeam(team);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改Team", exceptionMessage = "修改Team失败")
    @PostMapping("team/update")
    @ResponseBody
    @RequiresPermissions("team:update")
    public FebsResponse updateTeam(Team team) {
        this.teamService.updateTeam(team);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改Team", exceptionMessage = "导出Excel失败")
    @PostMapping("team/excel")
    @ResponseBody
    @RequiresPermissions("team:export")
    public void export(QueryRequest queryRequest, Team team, HttpServletResponse response) {
        List<Team> teams = this.teamService.findTeams(queryRequest, team).getRecords();
        ExcelKit.$Export(Team.class, response).downXlsx(teams, false);
    }

    @GetMapping("web/team/list")
    @ResponseBody
    public FebsResponse webTeamList(Team team) {
        List<Team> list= this.teamService.findTeams(team);
        List<Long> ids = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            ids.add(list.get(i).getUid());
        }
        List<User> data = userService.listByIds(ids);
        return new FebsResponse().success().data(data);
    }
    @PostMapping("web/team/addNewPeople")
    @ResponseBody
    public FebsResponse webAddNewPeople(String username,Long projectId) {
        User user = userService.findByName(username);
        if (user == null){
            return new FebsResponse().code(HttpStatus.NOT_FOUND);
        }else{
            List<Team> list = teamService.findProjectTeams(user.getUserId(),projectId);
            if (list.size()>0){
                return new FebsResponse().code(HttpStatus.FORBIDDEN);
            }
            Team team = new Team();
            team.setPid(projectId);
            team.setUid(user.getUserId());
            teamService.save(team);
            return new FebsResponse().success();
        }
    }

    /**
     * 根据项目Id查找团队成员
     * @param projectId
     * @return
     */
    @PostMapping("/web/team/getTeam")
    @ResponseBody
    public FebsResponse getTeam(Long projectId) {
        List<Long> idList = teamService.findProjectTeamsByProjectId(projectId);
        List<User> users = userService.listByIds(idList);
        for (int i = 0; i < users.size(); i++) {
            users.get(i).setPassword("*****");
        }
        return new FebsResponse().success().data(users);
    }

}
